<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="maximum-scale=1.0, minimum-scale=1.0, user-scalable=0, initial-scale=1.0, width=device-width"/>
<meta name="format-detection" content="telephone=no, email=no, date=no, address=no">
<title>Hello APP</title>
<link rel="stylesheet" type="text/css" href="../../css/aui-flex.css" />
<link rel="stylesheet" type="text/css" href="../../css/aui.css" />
<link rel="stylesheet" type="text/css" href="../../css/common.css" />
<style>
body{
	background: #F3F6F8;
}
.addrBox{
	background: #fff;
	padding: 0 1rem;
	margin-bottom: 0.8rem;
}
.comFlexItem textarea.textarea{
	height: 4.15rem;
	font-size: 0.7rem;
	padding-top: 0.1rem;
}
dl.align-start{
	padding-top: 0.6rem;
	-webkit-box-align: start;
	-webkit-align-items: flex-start;
	align-items: flex-start;
}
dl.comFlexItem dt {
		width: 4rem;
		font-size: 0.7rem;
		color: #373336;
}
select,
input.input {
		font-size: 0.7rem;
		height: 2rem;
}
.comFlexItem select,
.comFlexItem input.input {
		text-align: left;
}
.aui-switch:checked{
	border-color: #e73350;
	background-color: #e73350;
}
.btmFix{
	position: fixed;
	width: 100%;
	padding: 0 0.75rem;
	bottom: 0.75rem;
}
</style>
</head>
<body>
<div class="addrBox">
	<dl class="comFlexItem aui-border-b">
		<dt>收货人</dt>
		<dd class="flex-auto"><input type="text" name="" id="name" value="" placeholder="请输入姓名" class="input"></dd>
	</dl>
	<dl class="comFlexItem aui-border-b">
		<dt>手机号码</dt>
		<dd class="flex-auto"><input type="number" name="" id="phone" value="" placeholder="请输入手机号" class="input"></dd>
	</dl>
	<dl class="comFlexItem aui-border-b" tapmode onclick="address()">
		<dt>所在地区</dt>
		<dd class="flex-auto"><span class="color-999" id="prov">请选择</span></dd>
	</dl>
	<dl class="comFlexItem align-start">
		<dt>详细地址</dt>
		<dd class="flex-auto">
			<textarea class="textarea" name="name" id="address" rows="8" cols="80" placeholder="请输入详细地址">
		</textarea>
	</dd>
	</dl>
</div>
<div class="addrBox">
	<dl class="comFlexItem">
		<dt style="width:5rem">设为默认地址</dt>
		<dd class=""><input class="aui-switch" id="checkbox" type="checkbox" name="demo2"></dd>
	</dl>
</div>
<div class="btmFix">
	<div class="comBtmBtn radius" tapmode onClick="save()">保存地址</div>
</div>
</body>
<script type="text/javascript" src="../../script/import.js"></script>
<script type="text/javascript" src="../../script/common.js"></script>
<script type="text/javascript">
var addressId;
var postData = {};
var text1;
var text2;
var text3;
apiready = function(){
	api.parseTapmode();
	if(api.pageParam.newAddr){
		$$('.input').val('');
		$$('.textarea').val('')
	}
	addressId = api.pageParam.itemId;
	if(addressId){
		getInfo()
	}
	api.addEventListener({
			name: 'cityEvent'
	}, function(ret, err){
			if( ret ){
				text1 = ret.value.text1;
				text2 = ret.value.text2;
				text3 = ret.value.text3;
			}else{
					 alert( JSON.stringify( err ) );
			}
	});
};
//返回数据更改
function changeData(city){
	$$('#prov').text(city);
	if(city){
		$$('#prov').removeClass('color-999');
	}
}
function address(){
	api.openFrame({
		name: 'city_mask',
		url: 'city_mask.html',
		rect: {
			x: 0,
			y: 0,
			w: 'auto',
			h: 'auto'
		},
		pageParam: {
			wname: api.winName,
			fname: api.frameName
		}
	});
}
function getInfo(){
	api.ajax({
		url: BASE_URL + 'App/User/addressDetail',
		method: 'post',
		data: {
			values: {
				address_id: addressId
			}
		}
	},function(ret, err){
		if (ret) {
			if(ret.code == 1){
				console.log(JSON.stringify(ret))
				$$('#name').val(ret.data.address_username);
				$$('#phone').val(ret.data.address_phone);
				$$('#address').val(ret.data.address_detail);
				$$('#prov').text(ret.data.address_prov + '' +ret.data.address_city + '' +ret.data.address_district);
				text1 = ret.data.address_prov;
				text2 = ret.data.address_city;
				text3 = ret.data.address_district;
				if(ret.data.address_prov){
					$$('#prov').removeClass('color-999')
				}
				if(ret.data.address_default == 1){
					document.querySelector("input.aui-switch").checked = true
				}
			}
		}
	});
}
var defaul;
document.querySelector("input.aui-switch").addEventListener('click', function(){
	if (document.querySelector("input.aui-switch").checked) {
		defaul = 1
	}
});
var reg = /(^0{0,1}1[3|4|5|6|7|8|9][0-9]{9}$)/;
function save(){
	var name = $$('#name').val();
	var phone = $$('#phone').val();
	var prov = $$('#prov').text();
	var addr = $$('#address').val();
	if(name.length < 2 || name == ''){
		toast('名字不可小于两个字');
		return false;
	}
	if(!reg.test(phone)){
		toast('手机号格式错误')
		return false;
	}
	if(!text1){
		toast('请输入地址')
		return false;
	}
	if(!addr){
		toast('请输入详细地址')
		return false;
	}
	postData.username = name;
	postData.mobile = phone;
	postData.address_prov = text1;
	postData.address_city = text2;
	postData.address_district = text3;
	postData.address_detail = addr;
	postData.default = defaul;
	console.log(JSON.stringify(postData))
	if(addressId){
		console.log(1)
		edit();
	}else{
		add();
	}
}
function add(){
	postData.userid = getUserId();
	api.ajax({
		url: BASE_URL + 'App/User/addAddress',
		method: 'post',
		data: {
			values: postData
		}
	},function(ret, err){
		if (ret) {
			if(ret.code == 1){
				toast("添加成功");
				api.execScript({
					name: 'address_win',
					frameName: 'address_frm',
					script: 'getAdrList();'
				});
				setTimeout(function(){
					api.closeWin();
				},1000);
			}else{
				// toast(ret.msg);
			}
		}
	});
}
function edit(){
	postData.userid = getUserId();
	postData.address_id = addressId;
	console.log(JSON.stringify(postData))
	api.ajax({
		url: BASE_URL + 'App/User/editAddress',
		method: 'post',
		data: {
			values: postData
		}
	},function(ret, err){
		if (ret) {
			if(ret.code == 1){
				toast("修改成功");
				api.execScript({
					name: 'address_win',
					frameName: 'address_frm',
					script: 'getAdrList();'
				});
				setTimeout(function(){
					api.closeWin();
				},1000);
			}else{
				// toast(ret.msg);
			}
		}
	});
}
</script>
</html>
